﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.ZSJL.com.Net;
using System.Net.Mail;

namespace Service.ZSJL.Com.Net
{
    public class EmailService
    {
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="email">邮件</param>
        /// <param name="sendType">类型</param>
        public void Send(EmailModel email, SendType sendType)
        {
            SmtpClient smtp = new SmtpClient();
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;   //出站方式
            smtp.EnableSsl = false;    //smtp服务器是否启用SSL加密

            smtp.Host = email.Host;       //163服务器
            smtp.UseDefaultCredentials = true;    //验证账号密码  
            smtp.Credentials = new System.Net.NetworkCredential(email.SendAddress, email.SendPwd);

            MailMessage mm = new MailMessage(); //邮件类
            mm.Priority = MailPriority.Normal;        //邮件优先级
            //收件人看到的邮件来源
            mm.From = new MailAddress(email.SendAddress, email.SendName, Encoding.GetEncoding(936));//936简体中文


            //发送方式
            switch (sendType)
            {
                case SendType.Bcc:
                    //密送方式
                    mm.Bcc.Add(email.ReceiverAddress);
                    break;
                case SendType.CC:
                    //抄送方式
                    mm.CC.Add(email.ReceiverAddress);
                    break;
            }


            //邮件标题
            mm.Subject = email.Subject;
            mm.SubjectEncoding = Encoding.GetEncoding(936);

            //邮件正文
            mm.IsBodyHtml = true;   //邮件正文为html格式
            mm.BodyEncoding = Encoding.GetEncoding(936);

            mm.Body = email.Body;

            //附件
            if (!String.IsNullOrEmpty(email.Attachment))
            {
                mm.Attachments.Add(new Attachment(email.Attachment));
            }

            smtp.Send(mm);  //发送
        }
    }
}
